package com.cnbot.dchttpsdk.rx;

import android.content.Context;
import android.util.Log;

import com.cnbot.dchttpsdk.helper.OnSubscriberListener;
import com.cnbot.dchttpsdk.helper.OnSubscriberListenerImpl;

/**
 * Copyright (c) 2016-a  Hunan Cnbot Co., Ltd. All Rights Reserved.
 *
 * @descriptoin 默认的请求方式
 * @FileName: DefaultSubscriber.java
 * @author: dc
 * @date: 2019/1/19 14:10
 * @version: 1.0
 */

public class DefaultSubscriber extends BaseSubscriber<Object> {
    private final String TAG = "DefaultSubscriber";
    protected OnSubscriberListener mSubscriberOnNextListener;

    public DefaultSubscriber(OnSubscriberListener nextListener, Context context) {
        super(context);
        mSubscriberOnNextListener = nextListener;
    }

    /**
     * 将onNext方法中的返回结果交给Activity或Fragment自己处理
     *
     * @param o 创建Subscriber时的泛型类型
     */
    @Override
    public void onNext(Object o) {
        Log.v(TAG, "onNext");
        if (mSubscriberOnNextListener != null) {
            if (mSubscriberOnNextListener instanceof OnSubscriberListenerImpl) {
                ((OnSubscriberListenerImpl) mSubscriberOnNextListener).onSuccess(o);
            }
            if(o != null) {
                mSubscriberOnNextListener.onNext(o);
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        if (mSubscriberOnNextListener != null) {
            if (mSubscriberOnNextListener instanceof OnSubscriberListenerImpl) {
                ( mSubscriberOnNextListener).onError(e);
            }
            if(e != null) {
                mSubscriberOnNextListener.onError(e);
            }
        }
    }
}